package com.wuseguang.springcloud.service.impl;


import com.wuseguang.springcloud.Classcardservice.ClassCardFieldService;
import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.JdbcTemplate.BaseTemplate;
import com.wuseguang.springcloud.dao.mapper.SchoolMapper;
import com.wuseguang.springcloud.entity.School;
import com.wuseguang.springcloud.service.SchoolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @描述
 * @创建人 zm
 * @创建时间 2019/11/25
 */
@Service
public class SchoolServiceImpl implements SchoolService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private BaseTemplate baseTemplate;

    //推送接口
    @Autowired
    private ClassCardService classCardService;

    @Autowired
    private ClassCardFieldService service;

    @Override
    public void add(School school) {
        schoolMapper.insert(school);
        logger.info("school.getId()={}", school.getId());
    }

    @Override
    public void update(School school) {
        int i = schoolMapper.updateByPrimaryKeySelective(school);
        if (i > 0) {
            String sql = "SELECT id FROM classes WHERE school_id=?";
            List<Integer> integers = baseTemplate.queryListInteger(sql, new Object[]{school.getId()});
            //修改心跳并推送
            service.classesAndCards(integers, "classflag", "1");
            classCardService.classesAndCards(integers);
        }
    }

    @Override
    public School findOne(int id) {
        School school = schoolMapper.selectByPrimaryKey(id);
        return school;
    }

    @Override
    public void delete(Integer id) {
        schoolMapper.deleteByPrimaryKey(id);
    }
}
